﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.SixAcquaint.Model;
using Yipusoft.SixAcquaint.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using System.Collections.Generic;

public partial class BasicBase_KeyUnit_NewKeyUnit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
            if (userInfo.DepartmentID.Substring(0, 4) == "1300")
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') and DepartmentID<>'1300000' ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }
            else
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }

            DepartmentList.SelectedValue = department.DepartmentID.Substring(0, 4) + "000";
            UnitList.SelectedValue = department.DepartmentID;
            lblStandard.Text = KeyUnit.GetStandardInfoByID(Request["sid"]).Standard;
            //BindDepartment("201200", "201201", "201211");
            BindGrid(15, 0);
        }
    }

    void DepartmentListBind()
    {
        string strWhere = "departmentid like '" + DepartmentList.SelectedValue.Substring(0, 4) + "3%' or departmentid like '" + DepartmentList.SelectedValue.Substring(0, 4) + "1%' ";
        UnitList.DataSource = Department.GetList(strWhere);
        UnitList.DataBind();
    }

    private void BindDepartment(string permissionAdmin, string permissionLevel, string permissionUnit)
    {
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionAdmin))
        {
            foreach (DepartmentInfo department in Department.GetList("RIGHT(DepartmentID, 2) = '00'"))
            {
                DepartmentList.Items.Add(new ListItem(department.FullName, department.DepartmentID));
            }
        }
        else
        {
            DepartmentList.DataSource = GetDepartmentList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID, permissionLevel, permissionUnit);
            DepartmentList.DataTextField = "FullName";
            DepartmentList.DataValueField = "DepartmentID";
            DepartmentList.DataBind();

        }
    }
    private List<DepartmentInfo> GetDepartmentList(string departmentID, string permissionLevel, string permissionUnit)
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionLevel) || Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionUnit))
            departmentList = Department.GetList("DepartmentID='" + departmentID.Substring(0, 4) + "000'");
        return departmentList;
    }

    private void BindGrid(int pageSize, int pageIndex)
    {
        if (!panPage.Visible)
            panPage.Visible = true;
        string sid = Request["sid"];
        string departmnetID = UnitList.SelectedValue;
        string where = "DepartmentID like '" + departmnetID.Substring(0, 4) + "%' and StandardID='" + sid + "'";
        int totalCount = KeyUnit.GetKeyUnitCountByWhere(where);
        //pageIndex = pageIndex + 1;
        ViewState["PageCount"] = Convert.ToInt32(Math.Ceiling((double)totalCount / pageSize));
        if ((int)ViewState["PageCount"] < pageIndex)
            pageIndex = pageIndex - 1;
        ViewState["PageIndex"] = pageIndex;

        gvKeyUnits.DataSource = KeyUnit.GetKeyUnitList(where, "Addeddate", "desc", pageSize, pageIndex);

        gvKeyUnits.DataBind();
        lbGridInfo.Text = "共有<b>" + totalCount.ToString() + "</b>条记录 第<b>" + ((int)ViewState["PageIndex"] + 1).ToString() + "</b>页 共<b>" + ViewState["PageCount"].ToString() + "</b>页 每页<b>" + pageSize.ToString() + "</b>条";
        this.btnFirst.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnPrev.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnNext.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        this.btnLast.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        BindJumpList();
    }
    protected void JumpList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(15, int.Parse(JumpList.SelectedValue));
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = 0;
        BindGrid(15, (int)ViewState["PageIndex"]);
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] - 1;
        BindGrid(15, (int)ViewState["PageIndex"]);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] + 1;
        BindGrid(15, (int)ViewState["PageIndex"]);
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageCount"] - 1;
        BindGrid(15, (int)ViewState["PageIndex"]);
    }
    private void BindJumpList()
    {
        JumpList.Items.Clear();
        ListItem li;
        for (int i = 0; i < (int)ViewState["PageCount"]; i++)
        {
            li = new ListItem();
            li.Text = (i + 1).ToString();
            li.Value = i.ToString();
            JumpList.Items.Add(li);
        }
        JumpList.SelectedIndex = JumpList.Items.IndexOf(JumpList.Items.FindByValue(ViewState["PageIndex"].ToString()));
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201200") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201201") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201211"))
        {
            KeyUnitInfo keyUnitDtails = new KeyUnitInfo();
            keyUnitDtails.Address = txtAddress.Text;
            keyUnitDtails.DepartmentID = UnitList.SelectedValue;
            keyUnitDtails.East = txtEast.Text;
            keyUnitDtails.StandardID = Request["sid"];
            keyUnitDtails.Employee = int.Parse(txtPeople.Text == "" ? "0" : txtPeople.Text);
            keyUnitDtails.FaRenTel = txtCorpTel.Text;
            keyUnitDtails.FaRenDaiBiao = txtCorporation.Text;
            keyUnitDtails.FuZeRenTel = txtPricTel.Text;
            keyUnitDtails.GuDingZiChan = Convert.ToDecimal(txtAsserts.Text == "" ? "0" : txtAsserts.Text);
            keyUnitDtails.JingDu = Convert.ToDecimal(txtJingDu.Text == "" ? "0" : txtJingDu.Text);
            keyUnitDtails.WeiDu = Convert.ToDecimal(txtWeiDu.Text == "" ? "0" : txtWeiDu.Text);
            keyUnitDtails.JZhArea = Convert.ToDecimal(txtJZhArea.Text == "" ? "0" : txtJZhArea.Text);
            keyUnitDtails.NianZongChanZhi = Convert.ToDecimal(txtIncome.Text == "" ? "0" : txtIncome.Text);
            keyUnitDtails.North = txtNorth.Text;
            keyUnitDtails.South = txtSouth.Text;
            keyUnitDtails.SuoYouZhi = ddlPossessive.SelectedValue;
            keyUnitDtails.UnitName = txtUnitName.Text;
            keyUnitDtails.West = txtWest.Text;
            keyUnitDtails.XiaoFangFuZeRen = txtPrincipal.Text;
            keyUnitDtails.ZhDArea = Convert.ToDecimal(txtZDArea.Text == "" ? "0" : txtZDArea.Text);
            if (KeyUnit.InsertKeyUnit(keyUnitDtails) == 1)
            {
                lblInfo.Text = "添加重点单位基本数据成功。";
                BindGrid(15, 0);
            }
            else
            {
                lblInfo.Text = "添加失败！";
            }
        }
        else
            lblInfo.Text = "没有权限。";

    }

    protected void gvKeyUnits_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201203") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201200"))
        {
            if (KeyUnit.DeleteKeyUnit(gvKeyUnits.DataKeys[e.RowIndex].Value.ToString()))
                BindGrid(15, (int)ViewState["PageIndex"]);
        }
        else
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }
    }
    protected void DepartmentList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(15, 0);
    }
    protected void DepartmentList_SelectedIndexChanged1(object sender, EventArgs e)
    {
        DepartmentListBind();
        BindGrid(15, 0);
    }

    protected string getDepartmentName(object o)
    {
        if (o != null)
        {
            string DepartmentId = o.ToString();
            DepartmentInfo Info = Department.GetDepartment(DepartmentId);
            if (Info != null)
            {
                return Info.FullName;
            }
            else
            {
                return "";
            }
        }
        else
        {
            return "";
        }
    }
    protected void UnitList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(15, 0);
    }
}
